//
//  AddProjectViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/28.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  添加项目主界面

#import "AddProjectViewController.h"

@interface AddProjectViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)UITextField *projectNameDetailTF;

@property(nonatomic,strong)UITextView *projectDesTextView;

@end

@implementation AddProjectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor=WhiteColor;

    self.automaticallyAdjustsScrollViewInsets=NO;

    self.title=@"添加项目";

    [self.view addSubview:self.mainView];

    // 确认提交
    [AppUI initNavigationItem:self.navigationItem andWithText:@"确  认" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        if (self.projectNameDetailTF.text.length==0)
        {
            [self.view makeToast:@"请输入项目名称" duration:.5 position:CSToastPositionCenter];
        }
        else if (self.projectDesTextView.text.length==0)
        {
             [self.view makeToast:@"请输入项目描述" duration:.5 position:CSToastPositionCenter];
        }
        else
        {
            if (self.projectNameDetailTF.text.length>16)
            {
              [self.view makeToast:@"项目名称太长了" duration:1 position:CSToastPositionCenter];
            }
            else
            {
                [API AddProjectWithProjectName:self.projectNameDetailTF.text description:self.projectDesTextView.text success:^(id res) {

                    NSLog(@"sdhdfhfd00--%@",res);
                    
                    if ([res[@"rt"] isEqual:@(1)])
                    {
                        [self.view makeToast:@"添加成功" duration:1 position:CSToastPositionCenter];

                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                            [self.navigationController popViewControllerAnimated:NO];

                        });
                    }
                    else
                    {
                        [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                    }
                    
                    
                } failure:^(NSInteger stateCode) {
                    
                }];
            }
        }
    }];
}


-(UIView *)mainView
{
    if (!_mainView) {
        _mainView=[UIView new];
        _mainView=[[UIView alloc]initWithFrame:CGRectMake(0,64,APPW,0)];
        _mainView.backgroundColor=WhiteColor;
        setViewHeight_Add(_mainView,kmargin*2);

        // 项目名称
        UILabel *projectNameLabel=[self.mainView getLabelWithText:@"项目名称(不能修改)" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:projectNameLabel];
        projectNameLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);

        _projectNameDetailTF=[[UITextField alloc]initWithFrame:CGRectMake(kmargin*2,_mainView.frame.size.height,APPW-kmargin*4,kmargin*3)];
        _projectNameDetailTF.placeholder=@"请输入项目名称";
        // 修改placeholder颜色
        [_projectNameDetailTF setValue:LightGreyColor forKeyPath:@"_placeholderLabel.textColor"];
        _projectNameDetailTF.font=Font_Number(16);
        _projectNameDetailTF.delegate=self;
        _projectNameDetailTF.textColor=DarkGreyColor;
//        ViewBorderRadius(_projectNameDetailTF,2,1,MAIN_BACK_COLOR);
        [self.mainView addSubview:_projectNameDetailTF];
        setViewHeight_Add(_mainView,kmargin*4);

        // 项目描述
        UILabel *projectDesLabel=[self.mainView getLabelWithText:@"项目描述" font:Font_Number(16) textColor:DarkGreyColor textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        [self.mainView addSubview:projectDesLabel];
        projectDesLabel.frame=CGRectMake(kmargin*2,_mainView.frame.size.height,APPW/2,kmargin*2);
        setViewHeight_Add(_mainView,kmargin*2);


        _projectDesTextView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin*3/2,_mainView.frame.size.height,APPW-kmargin*3,APPH/5)];
//        ViewBorderRadius(_projectDesTextView,2,1,MAIN_BACK_COLOR);
        _projectDesTextView.placeholder=@"请输入项目描述";
        _projectDesTextView.font=Font_Number(16);
        _projectDesTextView.delegate=self;
        _projectDesTextView.textColor=DarkGreyColor;
        [self.mainView addSubview:_projectDesTextView];
        setViewHeight_Add(_mainView,APPH/5);
        
    }
    return _mainView;
}

// 点击return键时候键盘下去
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

@end
